package com.ideafactory.client.ybservice;

import android.app.Application;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.ideafactory.client.utils.FileConstants;
import com.ideafactory.client.utils.commonexception.MyUncaughtExceptionHandler;
//import com.ideafactory.client.utils.exception.MyCrashHandler;
import com.ideafactory.client.utils.preDefiniation;
import com.ideafactory.client.ybselfservice.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;

import rego.printlib.export.regoPrinter;


/**
 * Created by LiuShao on 2016/4/6.
 */
public class APP extends Application{

    private static APP mApplication;

    private boolean isNeedCaughtExeption = false;// 是否捕获未知异常

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        x.Ext.init(this);

        if (isNeedCaughtExeption) {
            cauchException();
        }
        initImageLoader();
    }
   public static ImageOptions adOptions;
    private void initImageLoader() {
        File file = new File(FileConstants.IMAGE_CACHE_PATH);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
                .threadPoolSize(3)//线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCacheFileCount(100) //缓存的文件数量
                .diskCache(new UnlimitedDiskCache(file))
                        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                        .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
//                       .writeDebugLogs() // Remove for release app
                        .build();//开始构建

        ImageLoader.getInstance().init(config);

        adOptions = new ImageOptions.Builder()
                .setImageScaleType(ImageView.ScaleType.FIT_XY)
                .setUseMemCache(false)
                .build();
    }

    public static ImageOptions getAdOptions() {
        return adOptions;
    }

    DisplayImageOptions options;
    public DisplayImageOptions getImageOption(){
        if(options==null){
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.loading_image) //设置图片在下载期间显示的图片
                    .showImageForEmptyUri(R.mipmap.image_load_failed)//设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.mipmap.image_load_failed)  //设置图片加载/解码过程中错误时候显示的图片
                    .cacheInMemory(false)//设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                    .resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
//                   .bitmapConfig(Bitmap.Config.ARGB_8888)
                    .bitmapConfig(Bitmap.Config.ALPHA_8)
                    .displayer(new SimpleBitmapDisplayer())
//                    .displayer(new FadeInBitmapDisplayer(300))//是否图片加载好后渐入的动画时间
                    .build();//构建完成
        }
        return options;
    }


    // -------------------异常捕获-----捕获异常后重启系统-----------------//
    private void cauchException() {
        // 程序崩溃时触发线程
//        MyCrashHandler mycrashHandler = new MyCrashHandler();
//        Thread.setDefaultUncaughtExceptionHandler(mycrashHandler);
    }

    public static APP getAPP(){
        return mApplication;
    }




    private preDefiniation.TransferMode printmode = preDefiniation.TransferMode.TM_NONE;
    private boolean labelmark = true;



    public void setPrintway(int printway) {

        switch (printway) {
            case 0:
                printmode = preDefiniation.TransferMode.TM_NONE;
                break;
            case 1:
                printmode = preDefiniation.TransferMode.TM_DT_V1;
                break;
            default:
                printmode =preDefiniation.TransferMode.TM_DT_V2;
                break;
        }

    }

    public int getPrintway() {
        return printmode.getValue();
    }

    public boolean getlabel() {
        return labelmark;
    }

    public void setlabel(boolean labelprint) {
        labelmark = labelprint;
    }
    

}
